<template>
  <div class="bg">
    <div class="login">
      <el-form :model="form" :rules="formrules" ref="formRefs" label-width="80px">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      formrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    reset() {
      this.$refs.formRefs.resetFields()
    },
    login() {
      this.$refs.formRefs.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$http({
            url: '/login',
            method: 'post',
            data: this.form
          })
          console.log(res)
          if (res.statusCode === 401) {
            this.$message.error('请输入正确的用户名或密码')
          }
          if(res.message==='登录成功'){
            this.$message.success(res.message)
            localStorage.setItem('token',res.data.token)
            localStorage.setItem('user',JSON.stringify(res.data.user))
            console.log(123)
            this.$router.push('/index')
            console.log(111)
          }

        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.bg{
  position: absolute;
  top:0;
  bottom: 0;
  width: 100%;
  background: url('../assets/bg.jpg') center;
  background-size:cover;
}
.login {
  width: 500px;
  height: 350px;
  position: absolute;
  background: rgba(255, 255, 255, 0.3);
  box-shadow: 0px 0px 20px skyblue;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 0 30px;
  display: flex;
  align-items: center;
  .el-form {
    width: 100%;
    margin-top: 40px;
  }
  .btn {
    display: flex;
    justify-content: center;
    margin-top: 60px;
    /deep/ .el-form-item__content {
      margin-left: 0px !important;
    }
  }
}
</style>